package org.example.kidscode.controller;

import org.example.kidscode.entity.ReservationSetting;
import org.example.kidscode.service.ReservationSettingService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/reservation-settings")
public class ReservationSettingController {

    private final ReservationSettingService settingService;

    public ReservationSettingController(ReservationSettingService settingService) {
        this.settingService = settingService;
    }

    @PostMapping
    public ResponseEntity<ReservationSetting> createSetting(@RequestBody ReservationSetting setting) {
        if (settingService.existsByCourseId(setting.getCourseId())) {
            throw new RuntimeException("Setting already exists for course: " + setting.getCourseId());
        }
        ReservationSetting savedSetting = settingService.saveSetting(setting);
        return ResponseEntity.ok(savedSetting);
    }

    @GetMapping("/{id}")
    public ResponseEntity<ReservationSetting> getSetting(@PathVariable String id) {
        ReservationSetting setting = settingService.getSettingById(id);
        return ResponseEntity.ok(setting);
    }

    @GetMapping("/by-course/{courseId}")
    public ResponseEntity<ReservationSetting> getSettingByCourse(@PathVariable String courseId) {
        ReservationSetting setting = settingService.getSettingByCourseId(courseId);
        return ResponseEntity.ok(setting);
    }

    @GetMapping
    public ResponseEntity<List<ReservationSetting>> getAllSettings() {
        List<ReservationSetting> settings = settingService.getAllSettings();
        return ResponseEntity.ok(settings);
    }

    @PutMapping("/{id}")
    public ResponseEntity<ReservationSetting> updateSetting(@PathVariable String id, @RequestBody ReservationSetting setting) {
        ReservationSetting updatedSetting = settingService.updateSetting(id, setting);
        return ResponseEntity.ok(updatedSetting);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteSetting(@PathVariable String id) {
        settingService.deleteSetting(id);
        return ResponseEntity.noContent().build();
    }
}